import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo07 {
    public static void main(String[] args) {
        Set<Dog> dogs = new TreeSet<>(new Comparator<Dog>() {
            @Override
            public int compare(Dog o1, Dog o2) {
                return o2.getAge() - o1.getAge();// 降序
            }
        });
        dogs.add(new Dog("老王", 12));
        dogs.add(new Dog("老王", 10));
        dogs.add(new Dog("老王", 14));

        for (Dog dog : dogs) {
            System.out.println(dog);
        }

    }
}

class Dog implements Comparable<Dog> {
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Dog o) {
        return this.age - o.getAge();// 升序
    }
}
